/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.system.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;
import pos.system.Role;

/**
 *
 * @author Shameera
 */
public class UserDataTable extends AbstractTableModel{
     
    private ResultSet results;
    List<String[]> userData = new ArrayList<String[]>();
    String[] columnNames = new String[]{DBConstant.CREDENTIAL_USERNAME, DBConstant.CREDENTIAL_ROLE, DBConstant.CREDENTIAL_DATE};

    public UserDataTable(ResultSet results) {
        setTable(results);
    }

    public void setTable(ResultSet res) {
        this.results = res;
        try {
            String[] temp;
            while (res.next()) {
                temp = new String[columnNames.length];
                temp[0] = res.getString(columnNames[0]);
                String role = res.getString(columnNames[1]);
                int role_id = Integer.valueOf(role);
                if(role_id == Role.ADMINSTRATOR.id){
                    temp[1] = Role.ADMINSTRATOR.name;
                }else if(role_id == Role.MANAGER.id){
                    temp[1] = Role.MANAGER.name;
                }else if(role_id == Role.EMPLOYEE.id){
                    temp[1] = Role.EMPLOYEE.name;
                }else if(role_id == Role.USER.id){
                    temp[1] = Role.USER.name;
                }
                temp[2] = res.getString(columnNames[2]);
                        
               
                userData.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDataTable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public int getRowCount() {
        return userData.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return userData.get(rowIndex)[columnIndex];
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
      
}
